//
//  IMTopView.m
//  QuantumWar
//
//  Created by mysj on 16/7/14.
//  Copyright © 2016年 zeng. All rights reserved.
//

#import "IMTopView.h"
#import <Masonry.h>
#import "NSString+Add.h"
#import "AppConfig.h"
#import "NSString+Add.h"

#define tp_sliderMag  10
#define tp_sliderandBgMagr  3


@interface IMTopView ()
@property (nonatomic, strong) UIButton    *tp_sjBtn;
@property (nonatomic, strong) UIButton    *tp_zyBtn;
@property (nonatomic, strong) UIButton    *tp_txBtn;
@property (nonatomic, strong) UIImageView *tp_ImgView;

@property (nonatomic, strong) UIImageView *tp_title_left;
@property (nonatomic, strong) UIImageView *tp_title_right;
@property (nonatomic, strong) UILabel     *tp_title;
@property (nonatomic, strong) UISlider    *tp_slider;
@property (nonatomic, strong) UIImageView *tp_slider_bg;
@property (nonatomic, strong) UILabel     *tp_slider_title;

@property (nonatomic, strong) UIButton *cuntBtn;

@property (nonatomic, copy ) NSString * text;

@end

@implementation IMTopView

- (void)setText:(NSString *)text {
    NSString *str = @"范围 5公里";
    NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:text];
    [titleText addAttribute:NSForegroundColorAttributeName value:RGB(226, 122, 55) range:NSMakeRange(2, str.length -2)];
    self.tp_title.attributedText = titleText;
}

- (void)didChanga:(UISlider *)slider {
    
    NSLog(@"%f", slider.value);
    CGFloat value = slider.value;
//    NSLog(@"%f",);
    CGFloat w = self.tp_slider_bg.frame.size.width;
    if (value == 0.0f) {
        [self upView:0 andOffset:5];
    } else if (value > 0.1 && value <= 1.4) {
        value = 0.96;
        [self upView:5 andOffset:w *.1];
    } else if (value > 1.4 && value <= 2.4){
        value = 1.93;
        [self upView:10 andOffset:w *.2];
    } else if (value > 2.4 && value <= 3.4){
        value = 2.91;
        [self upView:50 andOffset:w *.3];
    } else if (value > 3.4 && value <= 4.4){
        value = 3.93;
        [self upView:100 andOffset:w *.4];
    } else if (value > 4.4 && value <= 5.4){
        value = 4.98;
        [self upView:200 andOffset:w *.5];
    } else if (value > 5.4 && value <= 6.4){
        value = 5.96;
        [self upView:300 andOffset:w *.6];
    } else if (value > 6.4 && value <= 7.4){
        value = 6.95;
        [self upView:400 andOffset:w *.7];
    } else if (value > 7.4 && value <= 8.4){
        value = 7.93;
        [self upView:500 andOffset:w *.8];
    } else if (value > 8.4 && value <= 9.4){
        value = 8.93;
        [self upView:700 andOffset:w *.9];
    } else if (value > 9.4){
        value = 9.90;
        [self upView:1000 andOffset:w * .95];
    }
    [slider setValue:value animated:NO];
}

- (void)upView:(int)mag andOffset:(CGFloat)of {
    self.tp_slider_title.text = [NSString stringWithFormat: @"%d公里", mag];
    self.tp_title.attributedText = [self getTitleText:[NSString stringWithFormat: @"距离 %d公里", mag]];

    [self.tp_slider_title mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(of);
    }];
    
    [self.tp_title mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset([self getTitleOF]);
    }];
    
    [UIView animateWithDuration:0.3 animations:^{
        [self layoutIfNeeded];
    }];
}

- (void)selecredButn:(UIButton *)sender {
    self.cuntBtn.selected = NO;
    sender.selected = YES;
    self.cuntBtn = sender;
}

- (CGFloat)getTitleOF {
    CGSize size = [self.tp_title.attributedText boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
    return  ([UIScreen mainScreen].bounds.size.width *.5) - (size.width * .5);
}
- (NSMutableAttributedString *)getTitleText:(NSString *)str {
    NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:str];
    [titleText addAttribute:NSForegroundColorAttributeName value:RGB(226, 122, 55) range:NSMakeRange(2, str.length -2)];
    return titleText;
}

#pragma mark -
- (instancetype)init {
    if (self = [super init]) {
        [self prepareView];
    }
    return self;
}

- (void)prepareView {

    [self addSubview:self.tp_sjBtn];
    [self addSubview:self.tp_zyBtn];
    [self addSubview:self.tp_txBtn];
    [self addSubview:self.tp_ImgView];
    [self addSubview:self.tp_title];
    [self addSubview:self.tp_title_left];
    [self addSubview:self.tp_title_right];
    [self addSubview:self.tp_slider];
    [self addSubview:self.tp_slider_bg];
    [self addSubview:self.tp_slider_title];
    
    
    [self.tp_ImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.height.equalTo(@2);
    }];
    
    CGFloat w =  [UIScreen mainScreen].bounds.size.width / 3;
    [self.tp_sjBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tp_ImgView.mas_bottom);
        make.left.equalTo(self);
        make.height.equalTo(@34);
        make.width.equalTo(@(w));
    }];
    
    [self.tp_txBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right);
        make.top.equalTo(self.tp_ImgView.mas_bottom);
        make.size.equalTo(self.tp_sjBtn);
    }];
    
    [self.tp_zyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.tp_sjBtn.mas_right);
        make.right.equalTo(self.tp_txBtn.mas_left);
        make.top.equalTo(self.tp_sjBtn);
        make.size.equalTo(self.tp_sjBtn);
    }];
    
    CGFloat tp_title_lf = [self getTitleOF];
    [self.tp_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(tp_title_lf);
        make.top.equalTo(self.tp_zyBtn.mas_bottom).offset(tp_sliderMag);
    }];

    [self.tp_title_left mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tp_title).offset(-2);
        make.right.equalTo(self.tp_title.mas_left).offset(-tp_sliderMag);
    }];
    
    [self.tp_title_right mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tp_title).offset(-2);
        make.left.equalTo(self.tp_title.mas_right).offset(tp_sliderMag);
    }];
    
    [self.tp_slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(6.5);
        make.right.equalTo(self).offset(-3.5);
        make.top.equalTo(self.tp_title.mas_bottom).offset(tp_sliderMag);
    }];
    
    [self.tp_slider_bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tp_slider.mas_bottom);
        make.left.equalTo(self).offset(tp_sliderMag);
        make.right.equalTo(self).offset(-tp_sliderMag);
    }];
    
    [self.tp_slider_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tp_slider_bg.mas_bottom);
        make.left.equalTo(self).offset(tp_sliderMag *.5);
    }];
    
    self.tp_sjBtn.selected = YES;
    self.cuntBtn = self.tp_sjBtn;
}


- (UILabel *)tp_slider_title {
    if (!_tp_slider_title) {
        _tp_slider_title = [[UILabel alloc] init];
        _tp_slider_title.text = @"0公里";
        _tp_slider_title.textColor = RGB(226, 122, 55);
        _tp_slider_title.font = [UIFont systemFontOfSize:8];
    }
    return _tp_slider_title;
}


- (UILabel *)tp_title {
    if (!_tp_title) {
        _tp_title = [[UILabel alloc] init];
        _tp_title.textAlignment = NSTextAlignmentCenter;
        _tp_title.textColor = RGB(105, 215, 241);
        _tp_title.font = [UIFont systemFontOfSize:12];
       
        _tp_title.attributedText = [self getTitleText:@"范围 5公里"];
    }
    return _tp_title;
}

- (UIImageView *)tp_title_right {
    if (!_tp_title_right) {
        _tp_title_right = [[UIImageView alloc] init];
        _tp_title_right.image = [UIImage imageNamed:@"tp_title_left"];
        CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI);
        _tp_title_right.transform = transform;
    }
    return _tp_title_right;
}

- (UIImageView *)tp_title_left {
    if (!_tp_title_left) {
        _tp_title_left = [[UIImageView alloc] init];
        _tp_title_left.image = [UIImage imageNamed:@"tp_title_left"];
    }
    return _tp_title_left;
}

- (UIImageView *)tp_slider_bg {
    if (!_tp_slider_bg) {
        _tp_slider_bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tp_slider_bg"]];
    }
    return _tp_slider_bg;
}

- (UISlider *)tp_slider {
    if (!_tp_slider) {
        _tp_slider = [[UISlider alloc] init];
        [_tp_slider setMinimumTrackTintColor:[UIColor clearColor]];
        [_tp_slider setMaximumTrackTintColor:[UIColor clearColor]];
        [_tp_slider setMaximumValue:10];
        [_tp_slider setContinuous:NO];
//        _tp_slider.userInteractionEnabled = NO;
        [_tp_slider setThumbImage:[UIImage imageNamed:@"tp_slider_ThumbImg"] forState: UIControlStateNormal];
        [_tp_slider setThumbImage:[UIImage imageNamed:@"tp_slider_ThumbImg"] forState: UIControlStateHighlighted];
        [_tp_slider addTarget:self action:@selector(didChanga:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _tp_slider;
}

- (UIImageView *)tp_ImgView {
    if (!_tp_ImgView) {
        _tp_ImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top_view_bg"]];
    }
    return _tp_ImgView;
}

- (UIButton *)tp_txBtn {
    if (!_tp_txBtn) {
        _tp_txBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_tp_txBtn setBackgroundImage:[UIImage imageNamed:@"tp_txBtn_0"] forState:UIControlStateNormal];
        [_tp_txBtn setBackgroundImage:[UIImage imageNamed:@"tp_txBtn_0h"] forState:UIControlStateSelected];
        [_tp_txBtn addTarget:self action:@selector(selecredButn:) forControlEvents:UIControlEventTouchDown];
    }
    return _tp_txBtn;
}

- (UIButton *)tp_zyBtn {
    if (!_tp_zyBtn) {
        _tp_zyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_tp_zyBtn setBackgroundImage:[UIImage imageNamed:@"tp_zyBtn_0"] forState:UIControlStateNormal];
        [_tp_zyBtn setBackgroundImage:[UIImage imageNamed:@"tp_zyBtn_0h"] forState:UIControlStateSelected];
        [_tp_zyBtn addTarget:self action:@selector(selecredButn:) forControlEvents:UIControlEventTouchDown];
    }
    return _tp_zyBtn;
}

- (UIButton *)tp_sjBtn {
    if (!_tp_sjBtn) {
        _tp_sjBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_tp_sjBtn setBackgroundImage:[UIImage imageNamed:@"tp_sjBtn_0"] forState:UIControlStateNormal];
        [_tp_sjBtn setBackgroundImage:[UIImage imageNamed:@"tp_sjBtn_0h"] forState:UIControlStateSelected];
        [_tp_sjBtn addTarget:self action:@selector(selecredButn:) forControlEvents:UIControlEventTouchDown];
    }
    return _tp_sjBtn;
}

@end
